var app = getApp()
const baseUrl = app.globalData.baseUrl;
Page({
  data: {
    current: 0,
    showModal: true,
    product: [],
    productImg: [],
    // showPop: false,
    animationData: {},
    count: 1,
    swiperCurrent: 0,
    images: [],
    isCart: false,
    isSubscribe: "",
    showIndex: '',
    formats: []
  },
  changemodel: function(e) {
    this.setData({
      showModal: !this.data.showModal
    })
  },
  // 显示底部弹层
  showModal: function(e) {
    var _this = this;
    _this.setData({
      isCart: e.currentTarget.dataset.iscart
    })
    _this.setData({
      showPop: true
    })
  },
  // 隐藏底部弹层
  hideModal: function() {
    var _this = this;
    setTimeout(function() {
      _this.setData({
        showPop: false
      })
    })
  },
  swiperChange: function(e) {

    this.setData({
      swiperCurrent: e.detail.current // current 改变时会触发 change 事件

    })
  },
  onLoad: function(options) {
    var that = this;
    var productUrl = baseUrl + '/yq/product/info';
    var params = {
      productId: options.id,
    }
    var success = res => {
      var images = [];
      if (res.info.product.images) {
        images = res.info.product.images.substring(0, res.info.product.images.length - 1).split(";")
      } else {
        images.push(res.info.product.cover);
      }
      that.setData({
        product: res.info.product,
        images: images,
        formats: res.info.product.formats ? res.info.product.formats : [],
        isSubscribe: res.info.product.isSubscribe ? res.info.product.isSubscribe : 0
      })
      console.log("商品详情：", res)

    }
    var fail = res => {
      console.log("商品详情失败：", res)
    }
    app.request(productUrl, params, success, fail);

  },
  onShow() {

  },
  setTab: function(e) {
    const edata = e.currentTarget.dataset;
    this.setData({
      showIndex: edata.tabindex,

    })
  },
  //收藏按钮
  collectbtn: function(e) {
    var that = this;
    if (!this.data.isSubscribe) {
      var collectUrl = baseUrl + '/yq/user/subscribe';
      var params = {
        productId: this.data.product.id
      }
      var success = res => {
        console.log("收藏成功：", res)
        that.setData({
          isSubscribe: "1"
        })

      }
      var fail = res => {
        console.log(res)
        console.log("收藏失败")
      }
      app.request(collectUrl, params, success, fail);
    } else {
      var collectUrl = baseUrl + '/yq/user/unsubscribe';
      var params = {
        productId: this.data.product.id
      }
      var success = res => {
        console.log("取消收藏成功：", res)
        that.setData({
          isSubscribe: ""
        })

      }
      var fail = res => {
        console.log(res)
        console.log("取消收藏失败")
      }
      app.request(collectUrl, params, success, fail);
    }
  },

  returnIndex() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: function(res) {
        console.log(res)
        wx.openLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          scale: 18,
          success: function(res) {
            wx.chooseLocation({
              success: function(res) {
                console.log(res)
              },
            })
          }
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  addnum() {
    var that = this;
    this.data.count = this.data.count + 1
    that.setData({
      count: this.data.count
    })
  },
  subtractnum() {
    var that = this;
    if (that.data.count != 1) {
      this.data.count = this.data.count - 1
      that.setData({
        count: this.data.count
      })
    }
  },
  addcart(e) {
    console.log("点击购物车按钮", !this.data.product.formats)
    if (this.data.isCart == 1) { //点击购物车按钮
      if (this.data.showIndex === '') {
        console.log("没有选择参数");
        wx.showToast({
          title: '没有商品规格',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      } else {
        var that = this;
        var addCartUrl = baseUrl + '/yq/cart/update';
        var params = {
          productId: this.data.product.id,
          formatId: this.data.product.formats[this.data.showIndex].id,
          count: this.data.count
        }
        var success = res => {
          console.log("加入购物车：", res)
          wx.showToast({
            title: '加入购物车成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          this.hideModal();
        }
        var fail = res => {
          console.log("加入购物车失败：", res)
        }
        app.request(addCartUrl, params, success, fail);
      }
    } else { //点击立刻购买按钮
      if (this.data.showIndex === '') {
        wx.showToast({
          title: '没有商品规格',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      } else {
        var productItems = new Array;
        // this.data.product.formats=e.currentTarget.setData.tabindex;
        var formatTitle = this.data.product.formats[this.data.showIndex].title;
        productItems.push(this.data.product);
        wx.setStorageSync("formatTitle", formatTitle)
        wx.setStorageSync("product", JSON.stringify(productItems));
        wx.navigateTo({
          url: "../payfor/payfor?productId=1&formatId=" + this.data.product.formats[this.data.showIndex].id + "&count=" + this.data.count

        })
        this.hideModal();
      }
    }


  }
})